package com.example.game2048;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

public class MyCard extends FrameLayout {
	
	private int num = 0 ;
	private TextView textview;

	public MyCard(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public MyCard(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public MyCard(Context context) {
		super(context);
		init();
	}
	
	private void init(){
		textview = new TextView(getContext());
		textview.setTextSize(28);
		textview.setGravity(Gravity.CENTER);
		textview.setBackgroundColor(0x33ffffff);
		
		LayoutParams lp = new LayoutParams(-1, -1);
		lp.setMargins(10, 10, 10, 10);
		addView(textview, lp);
		setNum(0);
	}
	
	public int getNum(){
		return num;
	}

	public void setNum(int num){
		this.num = num;
		setText();
		setTextApperance();
	}
	
	public boolean equals(MyCard c){
		return getNum() == c.getNum();
	}
	
	public void setText() {
		if(num<=0){
			textview.setText("");
		}else{
			textview.setText(num+"");
		}
	}
	
	public void setTextApperance() {
		setTextColor(getNum());
		setTextBackgroundColor(getNum());
	}
	
	
	//设置Text的文字颜色
	public void setTextColor(int num) {
		switch(num) {
		case 2:
		case 4:
			textview.setTextColor(0xFF766E61);
			break;
		default:
			textview.setTextColor(0xFFF8F7F2);
			break;
		}
	}
	
	
	//设置Text文本的背景颜色
	public void setTextBackgroundColor(int num){
		switch (num) {
		case 2:
			textview.setBackgroundColor(0x33ffffff);
			break;
		case 4:
			textview.setBackgroundColor(0x33FFFFCC);
			break;
		case 8:
			textview.setBackgroundColor(0x33FF6666);
			break;
		case 16:
			textview.setBackgroundColor(0x33FF3300);
			break;
		case 32:
			textview.setBackgroundColor(0x33FF33FF);
			break;
		case 64:
			textview.setBackgroundColor(0x330033FF);
			break;
		case 128:
			textview.setBackgroundColor(0x3366FF66);
			break;
		case 256:
			textview.setBackgroundColor(0x3300CC33);
			break;
		case 512:
			textview.setBackgroundColor(0x33009933);
			break;
		case 1024:
			textview.setBackgroundColor(0x33CCCC00);
			break;
		case 2048:
			textview.setBackgroundColor(0x33CC0000);
			break;
		case 4096:
			textview.setBackgroundColor(0x33FFCC99);
			break;
		case 8192:
			textview.setBackgroundColor(0x33FFFF00);
			break;
		default:
			textview.setBackgroundColor(0x33C9BDAD);
			break;
		}
	}

}
